package com.dalao.root.custom.util;

import com.dalao.root.custom.entity.CusField;
import com.dalao.root.custom.entity.CusTable;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CustomBox {
    private StringBuilder message = new StringBuilder();

    public boolean checkTable(CusTable table) {
        if (table == null) {
            message.append("表数据不能为空\n");
        }
        if (StringUtils.isEmpty(table.getName())) {
            message.append("表名不能为空\n");
        }
        System.out.println("size:"+message.length());
        return message.length()==0;
    }

    public String getMessage() {
        return message.toString();
    }

    public Map<String,String> tableData(Map<String, String> data, List<CusField> fields) {
        Map<String,String> tableMap = new HashMap<>();

        //如果简称data在表的字段中，则添加进tableMap中
        for(Iterator<String> it = data.keySet().iterator(); it.hasNext();){
            String nameShort = it.next();
            for(CusField cusField:fields){
                if(cusField.getNameShort().equals(nameShort)){
                    tableMap.put(cusField.getName(),data.get(nameShort));
                    break;
                }
            }
        }

        return tableMap;
    }

    public List<Map<String,String>> whereData(List<Map<String,String>> where, List<CusField> fields) {
        for(int i = 0;i<where.size();i++){
            //取出每个where条件，判断字段名是否在列中
            Map<String,String> whereField = where.get(i);
            String whereShort = whereField.get("nameShort");
            for(int j = 0;j<fields.size();j++){
                String nameShort = fields.get(j).getNameShort();
                if(whereShort.equals(nameShort)){
                    whereField.put("name",fields.get(j).getName());
                    break;
                }else if("id".equals(whereShort)){
                    whereField.put("name","I_ID");
                }
            }
        }
        return where;
    }
}
